package correctionRecherche;

import java.util.HashMap;
import java.util.HashSet;

public class RercherPrefixe {

    private String[] ltsMots_bruts;
    HashMap<String, HashSet<String>> compRecherche;

    public RercherPrefixe(String query) {
        ltsMots_bruts = query.split(" ");

        compRecherche = new HashMap<String, HashSet<String>>();

        for (String s : ltsMots_bruts) {
            compRecherche.put(s, new HashSet<String>());
            compRecherche.get(s).addAll(IndexReader.getInstanceWith(s));
        }

    }

    public String getResultBool() {
        String str = "";
        for (String s : ltsMots_bruts) {
            if (compRecherche.containsKey(s)) {
                str += " ( ";
                for (int i = 0; i < compRecherche.get(s).size(); i++) {
                    if (i == compRecherche.get(s).size() - 1) {
                        str += " " + compRecherche.get(s).toArray()[i];
                    } else {
                        str += " OR " + compRecherche.get(s).toArray()[i];
                    }
                }
                str += " ) ";
            } else {
                str += " " + s;
            }
        }


        return str;
    }

    public String getResultStandard() {
        String str = "";
        for (String s : ltsMots_bruts) {
            if (compRecherche.containsKey(s)) {
                for (int i = 0; i < compRecherche.get(s).size(); i++) {
                    str += " " + compRecherche.get(s).toArray()[i];
                }
            } else {
                str += " " + s;
            }
        }


        return str;
    }
}
